package pl.edu.agh.mobicentsVoIP.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pl.edu.agh.mobicentsVoIP.core.User;
import pl.edu.agh.mobicentsVoIP.database.HSQLFacade;

public class LogInHttpServlet extends HttpServlet {

	public static final String DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";

	private String headWithTitle(String title) {
		return (DOCTYPE + "\n" + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n<body><img alt=\"Mobicents\" src=\"images/mobicentsVoIP_logo.jpg\" align=\"middle\" /><hr />");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String login = request.getParameter("login");
		String passwd = request.getParameter("passwd");
		PrintWriter out = response.getWriter();
		if (username != null && login != null && passwd != null) {
			HSQLFacade facade = new HSQLFacade();
			if (facade.getUser(username) == null
					&& facade.getUserByUserURI(login) == null) {
				User user = new User(username, passwd, login, false);
				facade.registerUser(user);
				out.println(headWithTitle("Rejestrowanie") + "<h3>Uzytkownik "
						+ username + " zarejestrowany</h3><hr>");
			}
		} else {

		}
	}

}
